package com.muqi.app.qlearn.student;

import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMContactManager;
import com.muqi.app.im.qmain.EMHelper;
import com.muqi.app.project.contants.MContants;
import com.muqi.app.project.utils.ShowToast;

import com.muqi.app.sharepreference.SharePreferenceUtil;
import com.wenhui.pedant.SweetAlert.SweetAlertDialog;
import com.wenhui.pedant.SweetAlert.SweetAlertDialog.OnSweetClickListener;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

/**
 * 
 */
public abstract class BaseFragment extends Fragment {
    protected BaseFragmentActivity mBaseActivity = null;
    protected Activity mActivity = null;
    public SharePreferenceUtil mSpUtil;
    private ProgressBar progressbar;
	private FrameLayout mFrameLayout;
	protected View emptyView;
	protected TextView emptyText;
    @SuppressWarnings("deprecation")
	@Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if(activity instanceof BaseFragmentActivity){
            this.mBaseActivity = (BaseFragmentActivity) activity;
        }
        this.mActivity = activity;
        mSpUtil = new SharePreferenceUtil(mActivity, MContants.UserLogin);
    }
    
    /**
	 * 展示progressbar
	 */
	public void showLoading() {
		showLoading(0, 0);
	}

	/**
	 * 展示progressbar
	 * 
	 * @param topSpace
	 *            progressbar与顶部的距离
	 * @param bottomSpace
	 *            progressbar与底部的距离
	 */
	public void showLoading(int topSpace, int bottomSpace) {
		if (progressbar != null) {
			if (!progressbar.isShown()) {
				FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) progressbar
						.getLayoutParams();
				if (layoutParams.topMargin != topSpace
						|| layoutParams.bottomMargin != bottomSpace) {
					layoutParams.topMargin = topSpace;
					layoutParams.bottomMargin = bottomSpace;
					progressbar.setLayoutParams(layoutParams);
				}
				mFrameLayout.setVisibility(View.VISIBLE);
				progressbar.setVisibility(View.VISIBLE);
			}
		} else {
			mFrameLayout = new FrameLayout(mActivity);
			mFrameLayout.setLayoutParams(new FrameLayout.LayoutParams(
					FrameLayout.LayoutParams.MATCH_PARENT,
					FrameLayout.LayoutParams.MATCH_PARENT));
			mFrameLayout.setOnClickListener(null);
			progressbar = new ProgressBar(mActivity);
			FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
					150, 150);
			progressbar.setLayoutParams(layoutParams);
			layoutParams.topMargin = topSpace;
			layoutParams.bottomMargin = bottomSpace;
			layoutParams.gravity = Gravity.CENTER;
			mFrameLayout.addView(progressbar);
			((FrameLayout) mActivity.getWindow().findViewById(
					Window.ID_ANDROID_CONTENT)).addView(mFrameLayout
					);
		}
	}

	/**
	 * 隐藏progressbar
	 */
	public void hideLoading() {
		if (progressbar != null) {
			mFrameLayout.setVisibility(View.GONE);
			progressbar.setVisibility(View.GONE);
		}
	}

	
	/**
	 * Activity跳转
	 * 
	 * @param cls
	 */
	public void startActivity(Class<?> cls) {
		Intent intent = new Intent(mActivity, cls);
		startActivity(intent);
	}
	
	public void addContact(final String toAddUsername){
		if(EMChatManager.getInstance().getCurrentUser().equals(toAddUsername)){
			new SweetAlertDialog(getActivity(), SweetAlertDialog.WARNING_TYPE)
			.setTitleText("不能添加自己")
			.setConfirmText("确定")
			.showCancelButton(false)
			.setConfirmClickListener(new OnSweetClickListener() {
				@Override
				public void onClick(SweetAlertDialog mDialog) {
					mDialog.dismissWithAnimation();
				}
			}).show();
			return;
		}
		if(EMHelper.getInstance().getContactList().containsKey(toAddUsername)){
		    //提示已在好友列表中(在黑名单列表里)，无需添加
		    if(EMContactManager.getInstance().getBlackListUsernames().contains(toAddUsername)){
		    	new SweetAlertDialog(getActivity(), SweetAlertDialog.WARNING_TYPE)
				.setTitleText("黑名单财友，无需添加")
				.setConfirmText("确定")
				.showCancelButton(false)
				.setConfirmClickListener(new OnSweetClickListener() {
					@Override
					public void onClick(SweetAlertDialog mDialog) {
						mDialog.dismissWithAnimation();
					}
				}).show();
		        return;
		    }
		    new SweetAlertDialog(getActivity(), SweetAlertDialog.WARNING_TYPE)
			.setTitleText("该财迷已是你的财友")
			.setConfirmText("确定")
			.showCancelButton(false)
			.setConfirmClickListener(new OnSweetClickListener() {
				@Override
				public void onClick(SweetAlertDialog mDialog) {
					mDialog.dismissWithAnimation();
				}
			}).show();
			return;
		}
		new Thread(new Runnable() {
			public void run() {
				try {
					//写死了个reason，实际应该让用户手动填入
					EMContactManager.getInstance().addContact(toAddUsername, "加个好友呗");
					getActivity().runOnUiThread(new Runnable() {
						public void run() {
							ShowToast.showShort(getActivity(), "好友申请已发送成功");
						}
					});
				} catch (final Exception e) {
					getActivity().runOnUiThread(new Runnable() {
						public void run() {
							
						}
					});
				}
			}
		}).start();
	}
	protected void showEmpty(String msg) {
		if (emptyView == null) {
			emptyText = (TextView) getView().findViewById(R.id.empty_tv);
			emptyText.setText(msg);
			emptyView = getView().findViewById(R.id.empty);
			emptyView.setVisibility(View.VISIBLE);
		} else {
			emptyView.setVisibility(View.VISIBLE);
		}

	}

	protected void hidEmpty() {
		if (emptyView == null) {
			emptyText = (TextView) getView().findViewById(R.id.empty_tv);
			emptyView = getView().findViewById(R.id.empty);
			emptyView.setVisibility(View.GONE);
		}else{
			emptyView.setVisibility(View.GONE);
		}
	}
}
